import 'package:cached_network_image/cached_network_image.dart';
import 'package:dss_base/dss_base.dart';
import 'package:dss_base/utils/log_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo_test/pages/test/demo_page.dart';
import 'package:flutter_demo_test/pages/home_viewmodel.dart';
import 'package:flutter_demo_test/pages/network/net_a_page.dart';
import 'package:flutter_demo_test/pages/network/net_b_page.dart';
import 'package:flutter_demo_test/widgets/my_button_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends BaseState<HomePage, HomeViewModel> {
  @override
  HomeViewModel onCreateViewModel() => HomeViewModel();

  void _btnClick(String value) {
    Toast.show('点击了: $value');
  }

  @override
  Widget onBuild(BuildContext context) {
    Widget content = ListView(
      children: [
        Row(
          children: [
            Expanded(
              child: _build_1('ic_1.png', 'Menu', () async {
                dynamic result = await NavigatorUtil.push(
                  () => DemoPage(name: '张三', age: 13),
                );
                Log.d('返回的数据：$result');
              }),
            ),
            Expanded(
              child: _build_1('ic_1.png', 'Menu', () async {
                dynamic result = await NavigatorUtil.push(
                  () => DemoPage(name: '李四', age: 14, friend: '翠花'),
                );
                Log.d('返回的数据：$result');
              }),
            ),
            Expanded(
              child: _build_1(
                'ic_1.png',
                'Menu',
                () => NavigatorUtil.push(() => NetAPage()),
              ),
            ),
          ],
        ),
        MyButtonWidget(text: '按钮1', onTap: ()=> _btnClick('1')),
        MyButtonWidget(text: '按钮2', backgroundGradient: buttonBackgroundYellow, onTap: ()=> _btnClick('2')),
      ],
    );
    return Scaffold(
      appBar: AppBarX(
        title: 'test_long'.tr,
        rightWidget: Row(
          children: [
            AppBarMenu(
              icon: 'ic_1.png',
              onPressed: () {
                Toast.show('点击了');
              },
            ),
            AppBarMenu(
              icon: 'ic_2.png',
              onPressed: () {
                Toast.show('点击了');
              },
            ),
          ],
        ),
      ),
      body: SafeArea(child: content),
    );
  }

  Widget _build_1(String icon, String text, GestureTapCallback onTap) {
    Widget content = Column(
      children: [
        SizedBox(
          width: 50.w,
          height: 50.w,
          child: ImageX(urlOrPath: icon),
        ),
        Text(
          text,
          style: TextStyle(color: ColorsX.color_181818, fontSize: 15.sp),
        ),
      ],
    );
    return ContainerX(onPressed: onTap, child: content);
  }
}
